<?php

/**
 * MVM_MALL 网上商店系统  报价单
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-03-02 $
 * $Id: quotation.php  www.mvmmall.com$
 * ---------------------------------------------
*/

require_once './include/common.inc.php';
if($action == 'goods_excel') {
	$search_sql   = false;
	$grade_array  = $cache->get_cache('grade');
	$grade_name[] = $lang['shop_user'];
	foreach ($grade_array as $val){
		if ($mm_show_price==1){
			$grade_name[]=$val['group_name'];
		} elseif ($mm_show_price==2 && $mm_group_id==$val['group_id']){
			$grade_name[]=$val['group_name'];
		} elseif ($mm_show_price==3 && $m_check_rank>=$val['degree']){
			$grade_name[]=$val['group_name'];
		}
	}
	$grade_name = implode(',',$grade_name);
	$excel_save = "NO.,$lang[buy_name],$lang[buy_img],$lang[code],$grade_name,$lang[explain]\n";
	$search_sql = " WHERE  upv = '1' AND up_date<='$m_now_time'";
	$result     = $db->query("SELECT * FROM `{$tablepre}goods_table` $search_sql");
	while($rt   = $db->fetch_array($result)) {
		$rt['goods_file1'] = MVMMALL_DIR.'/'.$rt['goods_file1'];
		$other_price       = user_price($rt);
	    $other_price[]     = currency($rt['goods_sale_price']);
		$rt['price']       = implode(',',$other_price);
		unset($other_price);
		$rt['goods_main']  = strip_tags(($rt['goods_main']));
		$excel_save       .= "$rt[uid],$rt[goods_name],$rt[goods_file1],$rt[goods_code],$rt[price],$rt[goods_main]\n";
	}
	$excel_save = iconv("UTF-8","gb2312","$excel_save");
	$excel_file = "goods_list".$m_now_counter.".csv";
	header("Content-typefile_type;charset=UTF-8");
	header("Content-Type: application/vnd.ms-excel;");
	header("Content-Disposition: attachment; filename=$excel_file");
	echo $excel_save;
	exit;
}

elseif($action=='list') {
	require_once MVMMALL_ROOT .'./include/pager.class.php';
	$mm_brand    = $cache->get_cache('brand');
	$grade_array = $cache->get_cache('grade');
	$order_sql   = false;
	$search_sql  = false;
	!$ps_ac && $ps_ac = "DESC";
	switch ($ps_or)
	{
		case 'price':
		{
			$order_sql = " ORDER BY goods_sale_price $ps_ac ";
			break;
		}
		case 'stock':
		{
			$order_sql = " ORDER BY goods_stock $ps_ac ";
			break;
		}
		case 'date':
		{
			$order_sql = " ORDER BY register_date $ps_ac ";
			break;
		}
		default:
		{
			$order_sql = ' ORDER BY uid DESC ';
			break;
		}
	}
	if($ps_ac == 'DESC') $ps_ac = 'ASC';
	elseif($ps_ac=='ASC') $ps_ac = 'DESC';
	else show_msg('pass_worng');
	$search_sql  = " WHERE  upv = '1' AND up_date<='$m_now_time'";
	$total_count = $db->counter("{$tablepre}goods_table","$search_sql");
	$page        = $page ? (int)$page : 1;
	$list_num    = 10;
	$rowset      = new Pager($total_count,$list_num,$page);
	$from_record = $rowset->_offset();
	$sql         =  "SELECT uid,price_sale,goods_category,goods_name,goods_file1,register_date,goods_sale_price,goods_stock,goods_brand
                      FROM `{$tablepre}goods_table` $search_sql $order_sql LIMIT $from_record, $list_num";
	$result      = $db->query($sql);
	while($rt    = $db->fetch_array($result)) {
		if ($rewrite == 1 && $rt['goods_brand']) {
			$rt['brand']    = "<a href=\"brand-view-$rt[goods_brand].html\" target=\"_blank\">".$mm_brand[$rt['goods_brand']].'</a>';
		} elseif ($rt['goods_brand']){
			$rt['brand']    = "<a href=\"brand.php?action=view&id=$rt[goods_brand]\" target=\"_blank\">".$mm_brand[$rt['goods_brand']].'</a>';
		}else {
			$rt['brand']    = '--';
		}
		$other_price        = user_price($rt);
		$rt['url']          = GetBaseUrl('product',$rt['uid']);
		$rt['title']        = $rt['goods_name'];
		$rt['subject']      = cut_str($rt['goods_name'],$mm_goods_length);
		$val['price']       = currency($rt['goods_sale_price']);
		$val['title']       = $lang['buy_price'];
		$other_price[]      = $val;
		$rt['price']        = $other_price;
		$rt['photo']        = $rt['goods_file1'];
		$rt['date']         = date($mm_date_format,$rt['register_date']);
		unset($rt['goods_name'],$rt['goods_file1'],$other_price);
		$goods[]            = $rt;
	}
	if($rewrite==1){
		$baseurl = str_replace('1.html','',GetBaseUrl('quotation',$action));
		$exc='.html';
	}else {
		$baseurl = GetBaseUrl('quotation',$action).'&page=';
	}
	$page_list  = $rowset->link($baseurl, $exc);
	$ps_ac     .= "&page=$page";//排序问题
	$navigation = make_navigation($lang['pricelist'],$baseurl.$page.$exc);//导航
	require_once MVMMALL_ROOT.'header.php';
	require_once template('quotation');
	footer();
}
